﻿
using DataService.Core.Common;
using DataService.Core.ServiceDTO;
using DataService.Data;
using DataService.Main;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTest
{
    /// <summary>
    /// 创建模型结构
    /// </summary>
    public class TestModelCode
    {
        private string GetPath()
        {
            string filePath = Path.Combine(FilePathHelper.BasePath, "MSSQL_Model.db");
            if (!File.Exists(filePath))
            {
                using (Stream stream = File.Create(filePath))
                {
                }
            }
            return filePath;
        }
        public void TestFromMSSQL_All()
        {
            //先从模型数据库获取 模型 表结构和表索引
            using (var sqldb = DataService.DBMangerFactory.CreateProvider(DatabaseType.SqlServer))
            {
                sqldb.ConnectionString = "数据库连接";

                var tables = ModelDAL.FindAllTables(sqldb);

                List<ModelInitData> modelInitDatas = new List<ModelInitData>();
                var configs = DAL.Query<Data.Model.SqlServer.PB_Config>(x => true, sqldb).ToList();
                var zoneDatas = DAL.Query<Data.Model.SqlServer.PB_Zone>(x => true, sqldb).ToList();
                var configSql = ModelDAL.FindBeatchInsertSql<Data.Model.SqlServer.PB_Config>(configs, sqldb);
                var zoneSql = ModelDAL.FindBeatchInsertSql<Data.Model.SqlServer.PB_Zone>(zoneDatas, sqldb);
                modelInitDatas.AddRange(configSql);
                modelInitDatas.AddRange(zoneSql);
                tables.ModelInitDatas = modelInitDatas;
                // string path = GetPath();
                string path = @"D:\MSSQL_Model.db";
                //创建模型文件
                using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.SQLite))
                {
                    #region sqlte地址

                    Console.WriteLine($"模型文件地址:{path}");
                    string connectionString = string.Format("Data Source={0};", path);
                    db.ConnectionString = connectionString;
                    #endregion
                    ModelDAL.CreateModelFileSchema(db);

                    db.Commit();
                }
                using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.SQLite))
                {
                    #region sqlte地址

                    Console.WriteLine($"模型文件地址:{path}");
                    string connectionString = string.Format("Data Source={0};", path);
                    db.ConnectionString = connectionString;
                    #endregion
                    Snowflake snowflake = new Snowflake();
                    foreach (var item in tables.Columns)
                    {
                        item.Id = snowflake.NewId();
                    }
                    foreach (var item in tables.Indexs)
                    {
                        item.Id = snowflake.NewId();
                    }
                    ModelDAL.FullModelFileSchema(tables, db);

                    db.Commit();
                }
            }


        }

        public void TestFromMSSQL()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.SqlServer))
            {
                db.ConnectionString = "连接字符串";

                ModelDAL.CreateModelCodes(@"D:\DataService\UnitTest\Model\", db, "PersonnelInfo");

            }
            Console.WriteLine("完成");
        }

        public void TestFormSqlite()
        {
            using (var db = DataService.DBMangerFactory.CreateProvider(DatabaseType.SQLite))
            {
                string connectionString = string.Format("Data Source={0};", @"D:\TestBigData.db");
                db.ConnectionString = connectionString;
                ModelDAL.CreateModelCodes(@"D:\框架代码\DataService\UnitTest\Model\", db, "PersonnelInfo");
            }
            Console.WriteLine("完成");
        }
    }
}
